//
// Created by REEJI2219 on 2024-08-07.
//
#include <iostream>
#include "seqListCpp.h"
using namespace std;
SeqList::SeqList(int size) {
    m_maxSize=size;
    //存储每个ElemType类型的数据
    m_pDataArr=new ElemType[size];
    m_length=0;
}
SeqList::~SeqList() {
    delete[] m_pDataArr;
    m_pDataArr=NULL;
}
bool SeqList::isEmpty() {
    return m_length==0;
}
int SeqList::getLength() {
    return m_length;
}
void SeqList::clearList() {
    for (int i=0;i<m_length;i++) {
        m_pDataArr = 0;
    }
}
bool SeqList::addElem(ElemType e) {
    if(m_length<m_maxSize) {
        *(m_pDataArr+m_length)=e;
        m_length++;
        return true;
    }
    return false;
}

bool SeqList::getElem(int i, ElemType *e) {
    if(i>=0 && i<m_maxSize) {
        //数据复制给e
        *e=m_pDataArr[i];
        return true;
    }
    return false;
}
bool SeqList::insertList(int i, const ElemType e) {
    if(m_length==m_maxSize-1) {
        resize();
    }
    if(i<0|| i>m_length-1) {
        return false;
    }
    for (int j=i;j<m_length-1;j++) {
        m_pDataArr[j+1]=m_pDataArr[j];
    }
    m_pDataArr[i]=e;
    return true;
}
bool SeqList::deleteElem(int i, ElemType *e) {

    if(i>=0 && i<m_length) {
        *e=m_pDataArr[i];
        for (int j=i;j<m_length;j++) {
            m_pDataArr[j]= m_pDataArr[j+1];
        }
        m_length--;
        return true;
    }
    return false;
}
void SeqList::traverseList() {
    for (int i=0;i<m_length;i++) {
        cout<<m_pDataArr[i]<<endl;
    }
}
int SeqList::length() {
    return m_length;
}

int SeqList::firstCompare(SeqList otherSeq) {
    ElemType target = 0;
    for (int i=0;i<m_length;i++) {
        for (int j=0;j<otherSeq.length();j++) {
            otherSeq.getElem(j,&target);
            if(m_pDataArr[i]==target) {
                return i;
            }
        }
    }
    return -1;
}
void SeqList::resize() {
    m_maxSize = m_maxSize*2;
    int* newm_pDataArr = new ElemType(m_maxSize);
    for(int i=0;i<m_length;i++) {
        newm_pDataArr[i]=m_pDataArr[i];
    }
    delete[] m_pDataArr;
    m_pDataArr = newm_pDataArr;
}
bool SeqList::margeList(SeqList other) {
    int size = this->length()+other.length();
    ElemType* newArra = new ElemType[size];
    int i=0;
    int j=0;
    int k=0;
    ElemType a;
    ElemType b;
    while (i<this->length() && j<other.length()) {
        a=this->getElem(i,&a);
        b=other.getElem(j,&b);
        if(a<b) {
            newArra[k]=a;
            i++;
        }else {
            newArra[k]=b;
            j++;
        }
        k++;
    }
    while (i<this->length()) {
        a=this->getElem(i,&a);
        newArra[k]=a;
        i++;
        k++;
    }
    while (j<other.length()) {
        b=other.getElem(j,&b);
        newArra[k]=b;
        i++;
        k++;
    }
    m_length=size;
    m_maxSize=size;
    delete[] m_pDataArr;
    m_pDataArr = newArra;
}


